package gui;

import javax.swing.*;
import java.awt.*;

/**
 * Created by Yaksha on 1/22/14.
 */
public class GridBagPanel extends JPanel {

    GridBagConstraints constraints = null;

    public GridBagPanel() {

        setLayout(new BorderLayout());
        JPanel wrapPanel = new JPanel(new GridBagLayout());
        constraints = new GridBagConstraints();

        JButton btn1 = new JButton("Button 1");
        constraints.ipadx = 8;
        constraints.ipady = 8;
        constraints.insets = new Insets(5, 5, 5, 5);
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.fill = GridBagConstraints.HORIZONTAL;
        wrapPanel.add(btn1, constraints);

        JButton btn2 = new JButton("Button 2");
        constraints.gridx = 0;
        constraints.gridy = 1;
        btn2.setSize(new Dimension(40, 20));
        wrapPanel.add(btn2, constraints);

        JButton btn3 = new JButton("Button 3");
        constraints.gridx = 1;
        constraints.gridy = 1;
        wrapPanel.add(btn3, constraints);

        JButton btn4 = new JButton("Button 4");
        constraints.gridx = 3;
        constraints.gridy = 1;
        constraints.fill = GridBagConstraints.NONE;
        wrapPanel.add(btn4, constraints);

        JTextField txtField = new JTextField();
        constraints.gridx = 1;
        constraints.gridy = 0;
        constraints.gridwidth = 3;
        wrapPanel.add(txtField, constraints);

        JTextArea txtArea = new JTextArea();
        txtArea.setLineWrap(true);

        resetConstraint();
        constraints.gridx = 0;
        constraints.gridy = 5;
        constraints.gridwidth = 4;
        constraints.gridheight = 4;
        constraints.fill = GridBagConstraints.BOTH;
        JScrollPane jScrollPane = new JScrollPane(txtArea);
        jScrollPane.setPreferredSize(new Dimension(280, 80));
        wrapPanel.add(jScrollPane, constraints);

        JScrollPane scrollPane = new JScrollPane(wrapPanel);
        add(scrollPane, BorderLayout.CENTER);

    }

    private void resetConstraint() {
        constraints.gridx = 0;
        constraints.gridy = 0;
        constraints.gridwidth = 1;
        constraints.gridheight = 1;
        constraints.anchor = GridBagConstraints.CENTER;
    }
}
